import numpy as np
import csv
import random
from datetime import datetime
from time import strftime
from scipy.stats import scoreatpercentile

'''
revision of test24
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate


def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    i = 0
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            print temp
            sKey.append(temp)
        if i > 10:
            break
        i += 1
            
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def build_data(inputFile):
    sKey = np.zeros(16)
    reader = csv.reader(open(inputFile, 'rb'), delimiter=' ')

    for record in reader:
        sKey[int(record[-2]) - 1992] += int(record[-1])

    #sKey = np.array(sKey)
    #sKey.shape=(-1, numColumn+2)
    return sKey

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    filePath = 'C:/_DATA/migration89_08/COUNTY Migration/SaTScan/SpaceTime/'
    #inputCSV = filePath + 'spacetime_total_in_case_con.cas'
    inputCSV = filePath + 'spacetime_total_in_pop_con.pop'
    iYear = range(1992,2008)
    year = []
    for item in iYear:
        year.append(str(item))
    #print year
    print inputCSV
    #print np.__version__
    data = build_data(inputCSV)
    print data, sum(data)


    fileLoc = filePath + 'temp1.csv'
    np.savetxt(fileLoc, data, delimiter=',', fmt = '%s')

    print "end at " + getCurTime()
    print "========================================================================"  

           
